/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sjtu.bean;

import com.sjtu.database.DataBase;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author p w
 */
public class LibraryBean {

    private int id;
    private String name;
    private String address;

    public LibraryBean(int id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static LibraryBean getLibrary(int libraryId) {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select * from library where id = '" + libraryId + "';");
            LibraryBean category = setLibrary(rs);
            rs.close();
            return category;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static LibraryBean setLibrary(ResultSet rs) {
        try {
            if (rs == null || !rs.next()) {
                return null;
            }
            return new LibraryBean(rs.getInt("id"), rs.getString("name"), rs.getString("address"));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static LibraryBean addLibrary(String name, String address) {
        try {
            DataBase database = DataBase.getInstance();
            String sql;
            ResultSet rs = database.query("select * from library where name = '" + name + "';");
            if (rs.next()) {
                return new LibraryBean(rs.getInt("id"), name, rs.getString("address"));
            }
            sql = "insert into library (name,address) values ('" + name + "','" + address + "');";
//            System.out.println(sql);
            database.update(sql);
            rs = database.query("select * from library where name = '" + name + "';");
            if (rs.next()) {
                return new LibraryBean(rs.getInt("id"), name, rs.getString("address"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static LibraryBean getByName(String libraryName){
        try {
            DataBase database = DataBase.getInstance();
            String sql;
            ResultSet rs = database.query("select * from library where name = '" + libraryName + "';");
            if (rs.next()) {
                return new LibraryBean(rs.getInt("id"), libraryName, rs.getString("address"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


}
